package com.team.arms.report.controller;

import com.team.arms.bingyi.domain.BingyiRegistInfo;
import com.team.arms.bingyi.mapper.BingyiRegistInfoMapper;
import com.team.arms.bingyi.service.BingyiRegistInfoService;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.report.dto.TownDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: WY
 * @date: 2022/6/5
 */
@RestController
@Api(tags = "大数据可视化模块")
@CrossOrigin
@RequestMapping("/data")
public class RegisterDataController {
    @Autowired
    private BingyiRegistInfoService bingyiRegistInfoService;

    @GetMapping("/town")
    @ApiOperation(value ="定兵数据/征兵潜力接口")
    public AjaxResult calculationTown(){
//        List<BingyiRegistInfo> list = bingyiRegistInfoMapper.selectList(null);
        List<BingyiRegistInfo> list = bingyiRegistInfoService.list();
        long count = bingyiRegistInfoService.count();
        //统计每个乡镇的个数
        Map<String, TownDTO> townMap=new HashMap<>();
        for (BingyiRegistInfo bingyiRegistInfo:list){
            String bingyiPlace = bingyiRegistInfo.getBingyiPlace();
            if(townMap.containsKey(bingyiPlace)){
                TownDTO townDTO = townMap.get(bingyiPlace);
                townMap.get(bingyiPlace).setNumber(townDTO.getNumber()+1);

                //判断乡镇中的年份有没有被取过
                Map<String, Integer> year = townDTO.getYear();
                if(year.containsKey(bingyiRegistInfo.getYear())){
                    year.replace(bingyiRegistInfo.getYear(),year.get(bingyiRegistInfo.getYear())+1);
                }else{
                    townDTO.getYear().put(bingyiRegistInfo.getYear(),1);
                }

            }else {
                TownDTO townDTO=new TownDTO();
                townDTO.setNumber(1);

                Map<String, Integer> yearMap=new HashMap<String, Integer>();
                yearMap.put(bingyiRegistInfo.getYear(),1);
                townDTO.setYear(yearMap);
                townMap.put(bingyiPlace,townDTO);
            }
        }

        for (String key:townMap.keySet()){
            TownDTO townDTO = townMap.get(key);
            String percent= String.valueOf((float)Math.round(((float) (townDTO.getNumber() *100)/(float) count)*10)/10);
            townMap.get(key).setPercent(percent);
        }

        return AjaxResult.success(townMap);
    }
}
